package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.CompetitionChangeRecordDO;
import com.example.makerspace.domain.MessageDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.MessageMapper;
import com.example.makerspace.service.AdminService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class MessageDAO extends BaseDAO<MessageDO> {

    private  final  static  String USERNAME="username";
    private  final  static  String ADMIN="admin";
    private  final  static  String TYPE="type";
    private  final   static  String  isRead="is_read";

    @Resource
    private MessageMapper messageMapper;
    @PostConstruct
    public void init(){
        this.mapper = messageMapper;
    }

    public IPage<MessageDO> selectByPage(Pagination pagination) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<MessageDO> selectByUserName(Pagination pagination, String username) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(USERNAME,username).orderByDesc("create_time");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<MessageDO> selectByType(Pagination pagination, String username, String type) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(TYPE,type);
         wrapper.and(qw->qw.eq(USERNAME,username).or().eq(USERNAME,ADMIN));
         wrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<MessageDO> selectByJustType(Pagination pagination, String type) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(TYPE,type);
        wrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<MessageDO> selectByLiskUsername(Pagination pagination, String username) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.like(USERNAME,username);
        wrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<MessageDO> selectByLiskUsernameAndType(Pagination pagination, String username, String type) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(TYPE,type).like(USERNAME,username).orderByDesc("create_time");;
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public  int  selectAllNoRead(String username){
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(isRead,0).eq(USERNAME,username)
                .or(qw->qw.eq(isRead,0).eq(USERNAME,ADMIN));
        wrapper.orderByDesc("create_time");
        List<MessageDO> messageDOS = mapper.selectList(wrapper);
        return messageDOS.size();
    }

    public List<MessageDO> select() {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
         wrapper.eq("type",0).orderByDesc("create_time");
        return  mapper.selectList(wrapper);
    }

    public int showAllNoticeUnRead(String username) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(isRead,0).eq(USERNAME,username).orderByDesc("create_time");
        List<MessageDO> messageDOS = mapper.selectList(wrapper);
        return messageDOS.size();
    }

    public void deleteByUsername(String username) {
        mapper.delete(new QueryWrapper<MessageDO>().eq(USERNAME,username));
    }

    public IPage<MessageDO> selectByUserNameAndAdmin(Pagination pagination, String username) {
        QueryWrapper<MessageDO> wrapper = new QueryWrapper<>();
        wrapper.eq(USERNAME,username).or().eq(USERNAME,"admin");
        wrapper.orderByDesc("create_time");
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);

    }
}
